package com.pachong.android.frameworkbase.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.EditText;

import com.pachong.android.frameworkbase.R;
import com.pachong.android.frameworkbase.customviews.iconcontent.IconTextArrowButton;


public class IconTextArrowEditText extends IconTextArrowButton {

    protected EditText mEtContent;

    public IconTextArrowEditText(Context context) {
        this(context, null);
    }

    public IconTextArrowEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mEtContent = new EditText(context);
        mEtContent.setBackgroundColor(0x00000000);
        mEtContent.setSingleLine(true);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1.0f);
        addSubView(mEtContent, params, Gravity.LEFT | Gravity.CENTER_VERTICAL);
        setupViews(attrs);
    }

    private float mEditTextHintSize = -1;

    private void setupViews(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.IconTextArrowEditText);


            // edit text hint color
            int hintColor = a.getColor(R.styleable.IconTextArrowEditText_arrowEditTextHintColor, -1);
            if (hintColor != -1) {
                mEtContent.setHintTextColor(hintColor);
            }

            // edit text hint size
            float editTextHintSize = a.getDimension(R.styleable.IconTextArrowEditText_arrowEditTextHintSize, -1);
            if (editTextHintSize != -1) {
                mEditTextHintSize = editTextHintSize;
            }

            // edit text hint
            String editHint = a.getString(R.styleable.IconTextArrowEditText_arrowEditTextHint);
            setHintText(editHint);

            // edit text size
            float editTextSize = a.getDimension(R.styleable.IconTextArrowEditText_arrowEditTextSize, -1);
            if (editTextSize != -1) {
                mEtContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, editTextSize);
            }

            // edit text hint color
            int editTextColor = a.getColor(R.styleable.IconTextArrowEditText_arrowEditTextColor, -1);
            if (editTextColor != -1) {
                mEtContent.setTextColor(editTextColor);
            }

            // edit text str
            String editStr = a.getString(R.styleable.IconTextArrowEditText_arrowEditText);
            setEditText(editStr);
            a.recycle();
        }
    }

    public void setEditText(CharSequence chars) {
        mEtContent.setText(chars);
    }


    public String getEditText() {
        return mEtContent.getText().toString();
    }


    public void setHintText(CharSequence chars) {
        if (!TextUtils.isEmpty(chars)) {
            SpannableString ss = setTextSize(chars, 0, chars.length(), (int) mEditTextHintSize, false);
            mEtContent.setHint(ss); // 一定要进行转换,否则属性会消失
        }
    }


    public static SpannableString setTextSize(CharSequence str, int start, int end, int value, boolean isDip) {
        if (start < 0 || start >= str.length() || end > str.length()) {
            throw new RuntimeException("文本处理越界 start == " + start + " end == " + end + " string length == " + str.length());
        }
        final SpannableString ss = new SpannableString(str);
        ss.setSpan(new AbsoluteSizeSpan(value, isDip), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // AbsoluteSizeSpan第二个参数代表value是不是dip值
        return ss;
    }


    public void showEditContent(boolean isHide) {
        if (isHide) {
            mEtContent.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            mEtContent.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    public void clearEditContent() {
        mEtContent.setText("");
    }
}
